package cn.uniquesoft.controller;

import cn.uniquesoft.controller.base.BaseController;
import cn.uniquesoft.page.DataGrid;
import cn.uniquesoft.service.IBankService;
import cn.uniquesoft.service.IVisitService;
import cn.uniquesoft.vo.BankVO;
import cn.uniquesoft.vo.VisitGatherVO;
import cn.uniquesoft.vo.VisitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping(value="/visitgather")
public class VisitGatherController{
    @Autowired
    private IVisitService visitService;
    @Autowired
    private IBankService bankService;

    @RequestMapping(value = "/index")
    public String index() {
        return "visitgather/index";
    }

    @RequestMapping(value = "/pageQuery1")
    @ResponseBody
    public DataGrid pageQuery1() {
        DataGrid dg = new DataGrid();

        VisitVO visitVO = new VisitVO();
        List<VisitGatherVO> vlist = new ArrayList<>();
        List<BankVO> blist = this.bankService.findAll(null);
        for(BankVO b : blist){
            VisitGatherVO v = new VisitGatherVO();
            //机构名称
            v.setBankname(b.getCname());
            System.out.println(v.getCname());
            Calendar cale = null;
            cale = Calendar.getInstance();
            int year = cale.get(Calendar.YEAR);
            int month = cale.get(Calendar.MONTH) + 1;
            String ym = year+"-"+month;

            visitVO.setIbank(b.getIid());
            List<VisitVO> visitVOS = this.visitService.findGather(visitVO);
            //获取累计走访数量
            v.setLjzfsl(visitVOS.size());
            int bhwdqysl = 0;
            int ngjjqysl = 0;
             int wfjjqysl = 0;
             int yjjjqysl = 0;
            for (int i= 0;i<visitVOS.size();i++){
                if(visitVOS.get(i).getInature() == 1){
                    bhwdqysl++;
                }
                if(visitVOS.get(i).getIsolve() == 1){
                    ngjjqysl++;
                }else{
                    wfjjqysl++;
                }
            }
            v.setBhwdqysl(bhwdqysl);
            v.setNgjjqysl(ngjjqysl);
            v.setWfjjqysl(wfjjqysl);
            v.setYjjjqysl(yjjjqysl);

            visitVO.setDvisitdate(ym);
            visitVOS = this.visitService.findGather(visitVO);
            //获取当月走访数量
            v.setDyzfsl(visitVOS.size());

            vlist.add(v);

        }
        int bhwdqyslhz = 0;
        int ngjjqyslhz = 0;
        int wfjjqyslhz = 0;
        int yjjjqyslhz = 0;
        int dyzfqislhz = 0;
        int ljzfqyslhz = 0;
        for(VisitGatherVO visitGatherVO : vlist){
            bhwdqyslhz+=visitGatherVO.getBhwdqysl();
            ngjjqyslhz+=visitGatherVO.getNgjjqysl();
            wfjjqyslhz+=visitGatherVO.getWfjjqysl();
            yjjjqyslhz+=visitGatherVO.getYjjjqysl();
            dyzfqislhz+=visitGatherVO.getDyzfsl();
            ljzfqyslhz+=visitGatherVO.getLjzfsl();
        }
        VisitGatherVO visitGatherVO1 = new VisitGatherVO();
        visitGatherVO1.setBankname("汇总");
        visitGatherVO1.setBhwdqysl(bhwdqyslhz);
        visitGatherVO1.setNgjjqysl(ngjjqyslhz);
        visitGatherVO1.setWfjjqysl(wfjjqyslhz);
        visitGatherVO1.setYjjjqysl(yjjjqyslhz);
        visitGatherVO1.setDyzfsl(dyzfqislhz);
        visitGatherVO1.setLjzfsl(ljzfqyslhz);
        vlist.add(visitGatherVO1);
        dg.setRows(vlist);
        dg.setTotal((long)vlist.size());


        return dg;
    }

}
